
package com.plian.system.service.fr.report.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.Tools.TokenUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.fr.financeindicators.FinanceIndicatorsDao;
import com.plian.system.dao.fr.payrolladjustment.PayrollAdjustmentDao;
import com.plian.system.entity.fr.report.ReportTableConfig;
import com.plian.system.mapper.fr.report.ReportTableConfigMapper;
import com.plian.system.service.fr.payrolladjustment.IPayrollVersionService;
import com.plian.system.service.fr.report.IReportTableConfigService;
import com.plian.system.service.fr.report.IReportVersionService;
import com.plian.system.service.sys.orgzation.OrgzationService;
import com.plian.system.vo.fr.report.ReportTableConfigVO;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 表格配置 前端用 服务实现类
 *
 * @author jianglei
 * @since 2019-10-23
 */
@Service
@AllArgsConstructor
public class ReportTableConfigServiceImpl extends ServiceImpl<ReportTableConfigMapper, ReportTableConfig> implements IReportTableConfigService {

    private IReportVersionService reportVersionService;

    private FinanceIndicatorsDao financeIndicatorsDao;

    private OrgzationService orgzationService;

    private PayrollAdjustmentDao payrollAdjustmentDao;

    private IPayrollVersionService payrollVersionService;

    /**
     * logger属性.
     */
    private static final Logger LOGGER  = LoggerFactory.getLogger(ReportTableConfigServiceImpl.class);

    private static String REPORT = "report";

    private static String VERSION = "version";

    @Override
    public ReportTableConfigVO getTableConfig(String year) {
        ReportTableConfig reportTableConfig = getOne(new LambdaQueryWrapper<ReportTableConfig>().eq(ReportTableConfig::getYear, year));
        List<PageData> list = new ArrayList<>();
        try {
            PageData pd = new PageData();
            if (StringUtil.isNotBlank(year) && year.contains(REPORT)){
                List<PageData> tempList = new ArrayList<>();
                pd.put(VERSION, reportVersionService.getVersion(year.split("_")[0]));
                tempList.addAll(financeIndicatorsDao.findbudgettype(pd));
                pd.put(VERSION, pd.get(VERSION) + StringPool.UNDERSCORE + REPORT);
                tempList.addAll(financeIndicatorsDao.findbudgettype(pd));
                PageData payrollPd = new PageData();
                payrollPd.put(VERSION, payrollVersionService.getVersion(year.split("_")[0]));
                List<PageData> payrollAdjustmentList = payrollAdjustmentDao.listAllOrg(payrollPd);
                for (PageData payroll : payrollAdjustmentList){
                    payroll.put("type", "payroll");
                }
                tempList.addAll(payrollAdjustmentList);
                JSONArray tableNames = JSON.parseArray(reportTableConfig.getTableNames());
                for (Object tableName : tableNames){
                    String str = (String)tableName;
                    for (PageData pageData : tempList){
                        if (str.equals(pageData.getString("orgId"))){
                            list.add(pageData);
                            break;
                        }
                    }
                }
            }else {
                pd.put(VERSION, reportVersionService.getVersion(year));
                list = financeIndicatorsDao.findbudgettype(pd);
            }
        }catch (Exception e){
            LOGGER.error("" + e);
        }

        ReportTableConfigVO vo = ReportTableConfigVO.builder()
                .tableConst(JSONArray.parseArray(reportTableConfig.getTableConst()))
                .tableNames(JSON.parseArray(reportTableConfig.getTableNames()))
                .colNames(JSON.parseArray(reportTableConfig.getColNames()))
                .required(JSON.parse(reportTableConfig.getRequired()))
                .rule(JSON.parseArray(reportTableConfig.getRule()))
                .reportOrgList(list)
                .notFillLines(getTab05Config(Integer.valueOf(year.contains(REPORT) ? year.split("_")[0] : year)))
                .build();

        return vo;
    }

    /**
     *表5-1 每家单位不可编辑行数
     * @return
     */
    private List<Integer> getTab05Config(int year){
        Map<String, List<Integer>> map = new HashMap<>();
        if (year >= 2022){
            map.put("宁波市轨道交通集团有限公司", Stream.of(13,14,15,19,20,21,23,24,25,28,29,30,31,32,33,34).collect(Collectors.toList()));
            map.put("宁波开发投资集团有限公司", Stream.of(11,12,13,15,18,19,20,22,23,24,25,29,30,31,32,33,34).collect(Collectors.toList()));
            map.put("宁波城建投资控股有限公司", Stream.of(11,12,19,20,21,22,23,24,25,28,30,31,32,33,34).collect(Collectors.toList()));
            map.put("宁波交通投资控股有限公司", Stream.of(11,13,14,15,19,20,21,22,23,24,25,29,30,31,32,33,34).collect(Collectors.toList()));
            map.put("宁波工业投资集团有限公司", Stream.of(11,12,13,14,18,19,20,21,22,23,24,25,27,29,30,31,32,33,34).collect(Collectors.toList()));
            map.put("宁波报业传媒集团有限公司", Stream.of(11,12,13,14,15,16,17,18,21,24,25,27,28,29,30,31,32,33,34).collect(Collectors.toList()));
            map.put("宁波广播电视集团", Stream.of(11,12,13,14,16,17,18,20,23,25,28,29,30,31,32,33,34).collect(Collectors.toList()));
            map.put("宁波城旅投资发展有限公司", Stream.of(11,12,19,20,21,22,23,24,25,28,30,31,32,33,34).collect(Collectors.toList()));
            map.put("宁波广电传媒集团有限公司", Stream.of(11,12,13,14,16,17,18,20,23,25,28,29,30,31,32,33,34).collect(Collectors.toList()));
            map.put("宁波通商集团有限公司", Stream.of(11,12,14,15,16,17,18,19,20,21,22,23,24,26,27,29,30,31,33,34).collect(Collectors.toList()));
            map.put("宁兴（集团）有限公司", Stream.of(11,13,14,15,16,18,19,20,21,22,23,24,25,28,29,30,31,32,33).collect(Collectors.toList()));
            map.put("宁波宁兴（集团）有限公司", Stream.of(11,13,14,15,16,18,19,20,21,22,23,24,25,28,29,30,31,32,33).collect(Collectors.toList()));
            map.put("宁波市商贸集团有限公司", Stream.of(11,12,13,14,17,18,19,20,21,22,23,24,25,27,28,29,32,33).collect(Collectors.toList()));
            map.put("宁波市文化旅游投资集团有限公司", Stream.of(11,12,13,14,15,16,19,20,21,23,24,28,29,30,31,32,33).collect(Collectors.toList()));
            map.put("宁波市水务环境集团有限公司", Stream.of(11,12,16,19,20,21,22,23,24,25,27,29,30,31,32,33,34).collect(Collectors.toList()));
            map.put("宁波市演艺集团有限公司", Stream.of(11,12,13,14,15,16,17,19,20,21,22,23,24,25,27,28,29,30,31,32,34).collect(Collectors.toList()));
        }else {
            map.put("宁波市轨道交通集团有限公司", Stream.of(13,14,15,16,19,20,21,23,24,25,28,30).collect(Collectors.toList()));
            map.put("宁波开发投资集团有限公司", Stream.of(11,12,13,15,18,19,20,22,23,24,25,29,30).collect(Collectors.toList()));
            map.put("宁波城建投资控股有限公司", Stream.of(11,12,19,20,21,22,23,24,25,28,29).collect(Collectors.toList()));
            map.put("宁波交通投资控股有限公司", Stream.of(11,13,14,15,19,20,21,22,23,24,25,29,30).collect(Collectors.toList()));
            map.put("宁波工业投资集团有限公司", Stream.of(11,12,13,14,18,19,20,21,22,23,24,25,27,29,30).collect(Collectors.toList()));
            map.put("宁波报业传媒集团有限公司", Stream.of(11,12,13,14,15,16,17,18,21,24,25,27,28,29,30).collect(Collectors.toList()));
            map.put("宁波广播电视集团", Stream.of(11,12,13,14,16,17,18,20,23,25,28,29,30).collect(Collectors.toList()));
            map.put("宁波城旅投资发展有限公司", Stream.of(11,12,19,20,21,22,23,24,25,28,29).collect(Collectors.toList()));
            map.put("宁波广电传媒集团有限公司", Stream.of(11,12,13,14,16,17,18,20,23,25,28,29,30).collect(Collectors.toList()));
            map.put("宁波通商集团有限公司", new ArrayList<>());
            map.put("宁波市文化旅游投资集团有限公司", new ArrayList<>());
        }

        String orgId = TokenUtil.getTokenOrgId();
        PageData pd = new PageData();
        pd.put("orgId", orgId);
        PageData groupOrg = orgzationService.getGroupbyId(pd).getPageData();
        if (groupOrg != null){
            String groupName = groupOrg.getString("name");
            if (map.containsKey(groupName)){
                return map.get(groupName);
            }
        }
        return new ArrayList<>();
    }
}
